package masterjava.common.client.gxt.highstock;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import masterjava.common.client.gwt.ExceptionHandler;
import masterjava.common.client.gxt.base.util.JsonObject;
import masterjava.common.shared.HasKey;
import masterjava.common.shared.state.StateException;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * User: gkislin
 * Date: 7/21/11
 */
public class HighStockType implements HasKey {


    private static final Map<String, HighStockType> CHART_TYPES_MAP = new LinkedHashMap<String, HighStockType>();

    private final String title;
    private final JsonObject params;

    public HighStockType(String title, JsonObject params) {
        this.title = title;
        this.params = params;
    }

    public static void init(String jsonConfiguration) {
        try {
            JsonObject configuration = new JsonObject(jsonConfiguration);
            for (String type : configuration.keySet()) {
                JSONObject typeConf = ((JSONObject) configuration.get(type));
                String title = ((JSONString) typeConf.get("title")).stringValue();
                JsonObject params = new JsonObject((JSONObject) typeConf.get("params"));
                CHART_TYPES_MAP.put(type, new HighStockType(title, params));
            }
        } catch (Exception e) {
            ExceptionHandler.treatException(new StateException("Illegal reporting configuration"));
        }
    }

    public JsonObject getParams() {
        return params;
    }

    public static Map<String, HighStockType> getTypesMap() {
        return CHART_TYPES_MAP;
    }

    public static HighStockType getType(String type) {
        return CHART_TYPES_MAP.get(type);
    }

    @Override
    public String getKey() {
        return title;
    }
}
